package com.leador.map.api.location.core;

import java.util.Random;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

/**
 *
 */
public class ClientInfoUtil {
	private static ClientInfoUtil instance = null;
	private static String deviceId = null;
	private static Context mAct = null;
	private static TelephonyManager telm;
	private static ConnectivityManager connManager;
	private static String PACKAGENAME;
	private static String RESSOLUTION;
	public static String Key = "";
	private static String imei = "";
	private static String imsi = "";
    private static String phonenum = "";
	// private static LocationProvider loc =null;

	public static ClientInfoUtil getInstance(Context act) {
		if (instance == null) {
			instance = new ClientInfoUtil();
			mAct = act;
			telm = (TelephonyManager) mAct
					.getSystemService(Context.TELEPHONY_SERVICE);
			connManager = (ConnectivityManager) mAct
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			PACKAGENAME = mAct.getApplicationContext().getPackageName();
			RESSOLUTION = initReslution();
			Key = getKey(mAct);
			imei = telm.getDeviceId();
			imsi = telm.getSubscriberId();
			phonenum = telm.getLine1Number();
		}
		return instance;
	}
	public static String getSdkVersion(){return String.valueOf(Build.VERSION.SDK_INT);}
	public static String getDevice(){return Build.DEVICE;}
	public static String getManufacturer(){return Build.MANUFACTURER;}
	public static String getPhonenum(){return phonenum;}
    public static String getImei(){
		return imei;
	}
	public static String getImsi(){
		return imsi;
	}
	public static String getPackageName() {
		return PACKAGENAME;
	}

	private static String initReslution() {
		DisplayMetrics dm = new DisplayMetrics();
		WindowManager winManager = (WindowManager) mAct
				.getSystemService(Context.WINDOW_SERVICE);
		winManager.getDefaultDisplay().getMetrics(dm);
		int width = dm.widthPixels;
		int height = dm.heightPixels;
		RESSOLUTION = height > width ? width + "*" + height : height + "*"
				+ width;
		return RESSOLUTION;
	}

	private ClientInfoUtil() {

		// this.loc = new LocationProvider(mCtx, Constant.Global.MAPKEY);
		// this.loc.start();
	}

	/**
	 * 是否在中国大陆
	 * @param Mcc
	 * @return
	 */
	public boolean isInMainLand(String Mcc) {
		if (null == Mcc || Mcc.equals("") || Mcc.equals("0")) {
			return true;
		}
		if (Mcc.equals("460") || Mcc.equals("461")) {
			return true;
		} else {
			return false;
		}
	}

	public String toPostString() {
		StringBuilder ret = new StringBuilder();
		ret.append("uid=");
		ret.append("\'" + this.getDeviceId() + "\'");
		ret.append("&sys_ver=");
		ret.append(this.getSysVersion());
		ret.append("&model=");
		ret.append(this.getModel());
		ret.append("&ntt=");
		ret.append(this.getNetworkType());
		ret.append("&bid=");
		ret.append(this.getBuildID());
		/*
		 * ret.append("&lat="); ret.append(""+loc.getLat());
		 * ret.append("&lng="); ret.append(""+loc.getLon());
		 */

		return ret.toString();
	}

	public String toStringClientInfo() {
		StringBuilder ret = new StringBuilder();
		ret.append("ia=1&");
		if (Key != null && Key.length() > 0) {
			ret.append("key=");
			ret.append(Key);
			ret.append("&");
		}
		ret.append("ct=android");

		String imei = telm.getDeviceId();
		String imsi = telm.getSubscriberId();

		ret.append("&ime=" + imei);
		ret.append("&sim=" + imsi);
		ret.append("&pkg=" + PACKAGENAME);

		ret.append("&mod=");
		ret.append(this.getModel());

		ret.append("&sv=");
		ret.append(this.getSysVersion());

		ret.append("&nt=");
		ret.append(this.getNetworkType());

		String str = telm.getNetworkOperatorName();
		ret.append("&np=");
		ret.append(str);
		ret.append("&ctm=" + System.currentTimeMillis());
		ret.append("&re=" + RESSOLUTION);
		ret.append("&av=" +"v1.0.0b20151231");
		ret.append("&pro=loc");

		return ret.toString();
	}

	public String toStringForAd() {
		StringBuilder ret = new StringBuilder();

		String imei = telm.getDeviceId();
		// String imsi =telm.getSubscriberId();

		ret.append("imei=" + imei);

		ret.append("&model_number=");
		ret.append(this.getModel().trim());

		ret.append("&os=Android");
		ret.append(this.getSysVersion());

		ret.append("&connection_type=");
		ret.append(this.getNetWorkWifiOrMobile());

		String str = telm.getSimOperator();
		ret.append("&operator_code=");
		ret.append(str);
		ret.append("&api_version=1&api_key=ETAGMEDIA");

		/*
		 * ret.append("&lat="); ret.append(""+loc.getLat());
		 * ret.append("&lng="); ret.append(""+loc.getLon());
		 */
		Log.d("inituser net", ret.toString());
		return ret.toString();
	}

	public String toPostJsonString() {
		StringBuilder ret = new StringBuilder();
		ret.append("'uid':");
		ret.append("\'" + this.getDeviceId() + "\'");

		return ret.toString();
	}

	public static String getSysVersion() {
		return Build.VERSION.RELEASE;
	}

	public static String getModel() {
		return Build.MODEL;
	}

	public static String getBuildID() {
		return Build.ID;
	}

	public String getNetworkType() {
		if (mAct.checkCallingOrSelfPermission("android.permission.ACCESS_NETWORK_STATE") !=PackageManager.PERMISSION_GRANTED) {
			return "";
		}

		if (connManager == null) {
			return "";
		}
		NetworkInfo netInfo = connManager.getActiveNetworkInfo();
		if (netInfo == null) {
			return "";
		}
		return netInfo.getTypeName();
	}

	public int getNetWorkWifiOrMobile() {

		NetworkInfo activeNetInfo = connManager.getActiveNetworkInfo();
		int activNetType = (activeNetInfo == null) ? -1 : activeNetInfo
				.getType();// 0: mobile 1:wifi

		int netType = -1;
		if (activNetType == 0)
			netType = telm.getNetworkType();
		return netType;
	}

	public String getDeviceIdEx() {
		Random rd = new Random();
		int r = rd.nextInt();
		String id = r + telm.getDeviceId();
		return id;
	}

	public String getDeviceId() {
		/*
		 * if ((((deviceId == null) || ("".equals(deviceId)))) &&
		 * (LBSApp.getApp(
		 * ).checkCallingOrSelfPermission("android.permission.READ_PHONE_STATE")
		 * == 0)) { TelephonyManager tm = LBSApp.getApp().getTelManager();
		 * 
		 * deviceId = tm.getSubscriberId(); }
		 * 
		 * if ((deviceId == null) || ("".equals(deviceId))) { deviceId =
		 * Settings.System.getString(LBSApp.getApp().getContentResolver(),
		 * "android_id"); }
		 * 
		 * if (deviceId == null) { deviceId = ""; }
		 * 
		 * if ((deviceId == null) || ("".equals(deviceId))) { Log.w("SMail",
		 * "cannot get device id."); }
		 */

		Random r = new Random();
		int x = r.nextInt();
		deviceId = telm.getDeviceId();
		return x + deviceId;
	}

	public static String getKey(Context context) {
		if (Key == null || Key.equals("")) {
			try {
				ApplicationInfo appInfo = context.getPackageManager()
						.getApplicationInfo(context.getPackageName(),
								PackageManager.GET_META_DATA);
				if(appInfo==null||appInfo.metaData==null){
					return Key;
				}
				Key = appInfo.metaData.getString("com.leador.apikey");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return Key;
	}

	public String getMcc(Context context) {
		String Mcc = "";
		TelephonyManager telephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		if (telephonyManager != null) {
			try {
				String stringNetworkOperator = telephonyManager
						.getNetworkOperator();
				Mcc = stringNetworkOperator.substring(0, 3);
			} catch (Exception e) {

			}
		}
		return Mcc;
	}

}
